﻿(function ($) {
    $.customerinfojs = {

        //保存新增客户信息
        saveCustomInfo: function (CustomDiYu, CustomName, CustomChejian, provience, city, area, CustomDetailAddress, CustomLng, CustomLat, CustomImg, customerstate, InstallDate, installstate, Reamrk, grid) {

            $.ajax({
                url: "./CustomerHandler.ashx",
                data: {
                    Action: "addCustomInfo",
                    CustomDiYu: CustomDiYu,
                    CustomName: CustomName,
                    CustomChejian: CustomChejian,
                    provience: provience,
                    city: city,
                    area: area,
                    CustomDetailAddress: CustomDetailAddress,
                    CustomLng: CustomLng,
                    CustomLat: CustomLat,
                    CustomImg: CustomImg,
                    customerstate: customerstate,
                    InstallDate: InstallDate,
                    installstate: installstate,
                    Reamrk: Reamrk
                },
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("保存成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();
                    }
                    else if (data == "falut") {
                        dialogMsg("保存失败", -1);
                    }
                    else if (data == "hasexit") {
                        dialogMsg("已存在此客户", 0);
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        //根据ID查找客户厂名   车间
        getCustomNameByID: function (ID) {

            var optionstring = "";
            $.ajax({
                url: "./CustomerHandler.ashx",
                data: {
                    Action: "getCustomNameByID",
                    ID: ID
                },
                async: true,
                type: "post",
                datatype: "json",
                success: function (data) {
                    Loading(false);
                    data = JSON.parse(data);
                    if (data.length > 0) {
                        console.log(data[0].CustomChejian);
                        $("#CustomChejian").html(data[0].CustomChejian);
                        $("#CustomName").html(data[0].CustomName);

                    }
                    else {
                        $("#CustomName").html("");
                        $("#CustomChejian").html("");
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                }
            });
        },

        //保存新增客户 购买设备卡号
        saveCustomerEquKaoHaoInfo: function (CustomerID, EquType, totalcount, EquNo, TaiHao, grid) {

            $.ajax({
                url: "./CustomerHandler.ashx",
                data: JSON.stringify({
                    Action: "saveCustomerEquKaoHaoInfo",
                    CustomerID: CustomerID,
                    EquType: EquType,
                    totalcount: totalcount,
                    EquNo: EquNo,
                    TaiHao: TaiHao
                }),
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("保存成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();

                    }
                    else if (data == "falut") {
                        dialogMsg("保存失败", -1);
                    }
                    else if (data == "hasexit") {
                        dialogMsg("已存在此卡号，请重新输入", 0);
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        //保存编辑客户信息
        editCustomInfo: function (CustomerID, CustomDiYu, CustomName, CustomChejian, provience, city, area, CustomDetailAddress, CustomLng, CustomLat, CustomImg, customerstate, InstallDate, installstate, Reamrk, grid) {

            $.ajax({
                url: "./CustomerHandler.ashx",
                data: {
                    Action: "editCustomInfo",
                    CustomerID: CustomerID,
                    CustomDiYu: CustomDiYu,
                    CustomName: CustomName,
                    CustomChejian: CustomChejian,
                    provience: provience,
                    city: city,
                    area: area,
                    CustomDetailAddress: CustomDetailAddress,
                    CustomLng: CustomLng,
                    CustomLat: CustomLat,
                    CustomImg: CustomImg,
                    customerstate: customerstate,
                    InstallDate: InstallDate,
                    installstate: installstate,
                    Reamrk: Reamrk
                },
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("保存成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();
                    }
                    else if (data == "falut") {
                        dialogMsg("保存失败", -1);
                    }
                    else if (data == "hasexit") {
                        dialogMsg("已存在此客户", 0);
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        uploadimage: function () {
            var formData = new FormData();
            formData.append('Action', "uploadimage");
            //var file = document.getElementById("file").files[0];
            formData.append('file', $('#file')[0].files[0]);
            console.log(formData);
            $.ajax({
                url: "./CustomerHandler.ashx",
                type: 'POST',
                data: formData,
                contentType: false,
                processData: false,
                mimeType: "multipart/form-data",
                success: function (data) {
                    console.log($.parseJSON(data));
                    var jsondata = $.parseJSON(data);
                    if (jsondata.status == true) {
                        $("#CustomImg").val(jsondata.path);
                        dialogMsg(jsondata.msg, 1);
                    }
                    else {
                        dialogMsg(jsondata.msg, -1);
                    }
                },
                error: function (data) {
                    dialogMsg("上传失败", -1);
                }
            });
        },

        deleteCustomerInfo: function (CustomerID, grid) {
            $.ajax({
                url: "./CustomerHandler.ashx",
                data: {
                    Action: "deleteCustomerInfo",
                    CustomerID: CustomerID
                },
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    console.log(data);
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("删除成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();
                    }
                    else if (data == "falut") {
                        dialogMsg("删除失败", -1);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        deleteCustomerEquInfo: function (CustomerEquID, grid) {
            $.ajax({
                url: "./CustomerHandler.ashx",
                data: {
                    Action: "deleteCustomerEquInfo",
                    CustomerEquID: CustomerEquID
                },
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    console.log(data);
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("删除成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();
                    }
                    else if (data == "falut") {
                        dialogMsg("删除失败", -1);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },


        //设置客户设备投入使用时间
        setCustomerUseDate: function (CustomerID, UseDate, grid) {
            $.ajax({
                url: "./CustomerHandler.ashx",
                data: {
                    Action: "setCustomerUseDate",
                    CustomerID: CustomerID,
                    UseDate: UseDate
                },
                type: "post",
                async: true,
                datatype: "json",
                success: function (data) {
                    if (data == "success") {
                        Loading(false);
                        dialogMsg("保存成功", 1);
                        grid.trigger("reloadGrid");
                        dialogClose();

                    }
                    else if (data == "falut") {
                        dialogMsg("保存失败", -1);
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                },
                beforeSend: function () {
                    Loading(true, "正在保存数据");
                },
                complete: function () {
                    Loading(false);
                }
            });
        },

        //监听客户需要维护的设备信息
        getCustomerMaintenanceInfo: function (CustomerID) {
            var optionstring = "有需要维护的设备";
            $.ajax({
                url: "../CustomerInfo/CustomerHandler.ashx",
                data: {
                    Action: "getCustomerMaintenanceInfo",
                    CustomerID: CustomerID
                },
                async: true,
                type: "post",
                datatype: "json",
                success: function (data) {
                    Loading(false);
                    data = JSON.parse(data);
                    console.log(data);
                    if (data != "") {
                        var _html = "<a href='#' class='linkhrefequ'>" + optionstring + "</a>";
                        dialogMsg(_html, 0);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                }
            });
        },

        //监听留言信息
        getMyMessage: function (userno) {
            var optionstring = "您有新的留言信息,请及时查看";
            $.ajax({
                url: "../CustomerInfo/CustomerHandler.ashx",
                data: {
                    Action: "getMyMessage",
                    userno: userno
                },
                async: true,
                type: "post",
                datatype: "json",
                success: function (data) {
                    Loading(false);
                    console.log(data);
                    if (data != "") {
                        var _html = "<a href='#' class='linkhref'>" + optionstring + "</a>";
                        dialogMsg(_html, 0);

                    }
                    
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                }
            });
        },

        //根据ID查找客户全部信息
        getCustomInfoByID: function (ID) {

            var optionstring = "";
            $.ajax({
                url: "./CustomerHandler.ashx",
                data: {
                    Action: "getCustomInfoByID",
                    ID: ID
                },
                async: true,
                type: "post",
                datatype: "json",
                success: function (data) {
                    Loading(false);
                    data = JSON.parse(data);
                    if (data.length > 0) {
                        console.log(data[0]);
                        $("#CustomDiYu").val(data[0].CustomDiYu);
                        $("#CustomName").val(data[0].CustomName);
                        $("#CustomChejian").val(data[0].CustomChejian);
                        $("#CustomDetailAddress").val(data[0].CustomDetailAddress);
                        $("#CustomLng").val(data[0].CustomLng);
                        $("#CustomLat").val(data[0].CustomLat);
                        $("#CustomImg").val(data[0].CustomImg);
                        $("#InstallDate").val(data[0].InstallDate);
                        $("#Reamrk").val(data[0].Reamrk);
                        if (data[0].CustomeState == 1) {
                            $("#optionsRadios1").checked = true;
                        }
                        else {
                            $("#optionsRadios2").checked = true;
                        }
                        var str = data[0].CustomProvience + ',' + data[0].CustomCity + ',' + data[0].CustomArea;
                        var select = $('.city-picker-select').cityPicker({
                            dataJson: cityData,
                            renderMode: false,
                            autoSelected: true,
                            level: 3,
                            onChoiceEnd: function () {
                                console.log(this.values)
                            }
                        })
                        select.setCityVal(str);
                        //select.changeStatus('disabled');
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    Loading(false);
                    dialogMsg(errorThrown, -1);
                }
            });
        }
    };


    $(function () {

    });
})(jQuery);